// routes 有原本默认的静态路由和动态路由，直接引入即可
import { constantRoutes, asyncRoutes } from '@/router'

const state = {
  // 初始化的时候，默认静态路由
  routes: constantRoutes
}

const mutations = {
  // 动态添加路由
  setAsyncRoutes(state, newAsyncRoutes) {
    state.routes = [
      ...constantRoutes,
      ...newAsyncRoutes
    ]
  }
}

const actions = {
  async filterRoutes(ctx, menus) {
    // 将 menus 与 asyncRoutes 中字段匹配，返回该用户的真正动态路由权限
    const newAsyncRoutes = asyncRoutes.filter(item => menus.includes(item.children[0].meta.permission))
      // 将拿到的权限路由数据添加到 vuex
    ctx.commit('setAsyncRoutes', newAsyncRoutes)
    return newAsyncRoutes
  }
}

const getters = {}

export default {
  namespaced: true,
  state,
  mutations,
  actions,
  getters
}
